// Copyright (c) 2011 The Sitsac Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

function isAchorElement(elem) {
  return elemIsAnchor = elem.tagName == 'a' || elem.tagName == 'A';
}

function isSpaceTextElement(elem) {
  return elem.nodeType == 3 && elem.textContent.trim().length ==0;
}

function matchesPageURL(elem) {
  if (isAchorElement(elem))
    return elem.href.match(/(page=[0-9]+|start=[0-9]+|p=[0-9]+)/);
  
  return false;
}

function matchesPageText(elem) {
  return elem.textContent.trim().match(/^[?\[0-9]+\]?$/);
}


function isCurrentPage(elem, pageNo, currentURL) {
  var matchedNumericText = elem.textContent.trim().match(/^[?\[0-9]+\]?$/);

  if (matchesPageText(elem))
    return isAchorElement(elem) ? currentURL == elem.href :
        elem.textContent.trim().match(/[0-9]+/)[0] == pageNo;
  
  return false;	
}

function findCurrentPageInPrevSibling(elem, pageNo, CurrentURL) {
  for (var prev = elem.previousSibling; prev; prev = prev.previousSibling) {
    if  (isSpaceTextElement(prev)) 
	    continue;

  if (isCurrentPage(prev, pageNo, CurrentURL))
    return prev;
  }
  return null;
}

function findCurrentPageInNextSibling(elem, pageNo, CurrentURL) {
  for (var next = elem.nextSibling; next; next = next.nextSibling) {
    if  (isSpaceTextElement(next)) 
      continue;

    if (isCurrentPage(next, pageNo, CurrentURL))
      return next;
  }
  return null;
}

function matchesPageLink(elem) {
  return matchesPageURL(elem) && matchesPageText(elem);
}

var nextURL = '';
var prevURL = '';

function findLinks() {
  var anchorTags = document.getElementsByTagName('a');

  for (var i = 0, anchor; anchor = anchorTags[i]; i++) {
    if (matchesPageLink(anchor)) {
      var pageNo = anchor.text.trim().match(/[0-9]+/)[0];
      var curPageElemInPrev = findCurrentPageInPrevSibling(anchor, parseInt(pageNo)-1, location.href);
      if (curPageElemInPrev) {
        nextURL = anchor.href;
        for (var prev = curPageElemInPrev.previousSibling; prev; prev = prev.previousSibling) {
          if  (isSpaceTextElement(prev)) 
            continue;
          if (matchesPageURL(prev))
            prevURL = prev.href;
          return;
        }
      }
      var curPageElemInNext = findCurrentPageInNextSibling(anchor, parseInt(pageNo)+1, location.href);
      if (curPageElemInNext) {
        prevURL = anchor.href;
        for (var next = curPageElemInNext.nextSibling; next; next = next.nextSibling) {
          if  (isSpaceTextElement(next)) 
            continue;
          if (matchesPageURL(next))
            nextURL = next.href;
          return;
        }
      }
    }
  }
}

findLinks();


window.addEventListener("keydown", function(event) {
  // Bind to both command (for Mac) and control (for Win/Linux)
  var modifier = event.ctrlKey || event.metaKey;
  if (modifier && event.shiftKey) {
    if (prevURL && event.keyCode == 90) {		// 'Z'
      chrome.extension.sendRequest({redirect: prevURL});
    }
    else if (nextURL && event.keyCode == 88) {	// 'X'
      chrome.extension.sendRequest({redirect: nextURL});
    }
  }
}, false);
